/**
 * default progress bar
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 11. 20 
*/
package com.cmiscm.videos.stream
{
	import com.cmiscm.utils.common.NumberUtilities;
	import com.cmiscm.utils.display.MakeButton;
	import com.cmiscm.videos.VideoPlayer;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
		
	public class CMprogress extends Sprite
	{
		private var _stage:Stage;
			
		private var _be_default:Boolean;
		private var _bufferLimit:int;
		
		private var _progess_in:Shape;
		private var _buffer_in:Shape;
		private var _progess:Sprite;
		private var _buffer:Sprite;
		private var _bg:Shape;
		private var _slideBt:MakeButton;
		private var _slideWidth:int;
		
		private var _be_seek:Boolean;
		
		private var _refer:CMstream;
		
		public function CMprogress($stream:CMstream)
		{
			super();
			setting($stream);
		}
		private function setting($stream:CMstream):void
		{
			_refer = $stream;
		}
		
		/**
		 * init 
		 * 
		 */		
		public function init():void
		{
			_progess.width = 0;
			_buffer.width = 0;
		}
		
		/**
		 * clear 
		 */		
		public function clear():void
		{
			_buffer.scaleX = 0;
			_progess.scaleX = 0;
		}
				
		/**
		 * destroy 
		 */		
		public function destroy():void
		{
			this.removeChild(_bg);
			this.removeChild(_buffer);
			this.removeChild(_progess);
			
			_progess.removeChild(_progess_in);
			_progess = null;
			_progess_in.graphics.clear();
			_progess_in = null;
			
			_buffer.removeChild(_buffer_in);
			_buffer = null;
			_buffer_in.graphics.clear();
			_buffer_in = null;
			
			_bg.graphics.clear();
			_bg = null;
			
			if (_slideBt != null) {
				this.removeChild(_slideBt);
				_slideBt.removeEventListener(MouseEvent.MOUSE_DOWN, onSlideDown);
				_slideBt = null;
				if (_slideBt.hasEventListener(Event.ENTER_FRAME)) _slideBt.removeEventListener(Event.ENTER_FRAME, onSlideMove);
				if (_stage.hasEventListener(MouseEvent.MOUSE_UP)) _stage.removeEventListener(MouseEvent.MOUSE_UP, onSlideUp);
				_stage = null;
			}
		}
		
		/**
		 * update 
		 * 
		 */		
		public function update($bufferPercent:Number, $currentPercent:Number):void
		{
			_buffer.scaleX = $bufferPercent;			
			_progess.scaleX += ($currentPercent - _progess.scaleX) * 0.3;
		}
		
		/**
		 * get 
		 */		
		public function set stage($stage:Stage):void
		{
			_stage = $stage;
		}
		
		/**
		 * change progress width 
		 */		
		public function setWidth($tW:int):void
		{
			_slideWidth = $tW;
			
			_progess_in.width = $tW;
			_buffer_in.width = $tW;
			_bg.width = $tW;
			
			_slideBt.button.width = $tW;
		}
		
		/**
		 * draw default progress 
		 */				
		public function addDefaultProgress($tW:int, $tH:int, $progress_color:uint, $buffer_color:uint, $bg_color:uint, $be_control:Boolean=false, $control_tY:int=0, $control_tH:int=0, $be_hand:Boolean=false, $buffer_limit:int=0):void
		{
			_be_default = true;
			_bufferLimit = $buffer_limit;
			
			_slideWidth = $tW;
						
			// draw
			_progess_in = new Shape();
			_progess_in.graphics.beginFill($progress_color);
			_progess_in.graphics.drawRect(0, 0, _slideWidth, $tH);
			_progess_in.graphics.endFill();
			_progess = new Sprite();
			_progess.addChild(_progess_in);
			
			_buffer_in = new Shape();
			_buffer_in.graphics.beginFill($buffer_color);
			_buffer_in.graphics.drawRect(0, 0, _slideWidth, $tH);
			_buffer_in.graphics.endFill();
			_buffer = new Sprite();
			_buffer.addChild(_buffer_in);
			
			_bg = new Shape();
			_bg.graphics.beginFill($bg_color);
			_bg.graphics.drawRect(0, 0, _slideWidth, $tH);
			_bg.graphics.endFill();
			
			this.addChild(_bg);
			this.addChild(_buffer);
			this.addChild(_progess);
			
			if ($be_control) {
				_slideBt = new MakeButton(_slideWidth, $control_tH);
				_slideBt.y = $control_tY;
				_slideBt.addEventListener(MouseEvent.MOUSE_DOWN, onSlideDown);
				this.addChild(_slideBt);
			}
			
			init();
		}
		
		/**
		 * slide event 
		 */		
		private function onSlideDown(evt:MouseEvent):void
		{
			_be_seek = true;
			_refer.stream.pause();	
			_refer.seek(NumberUtilities.getCurrent(checkLimitSlide(evt.currentTarget.mouseX), 0, _slideWidth, 0, _refer.duration));
			
			_slideBt.addEventListener(Event.ENTER_FRAME, onSlideMove, false, 0, true);			
			_stage.addEventListener(MouseEvent.MOUSE_UP, onSlideUp, false, 0, true);			
		}
		private function onSlideMove(evt:Event):void
		{
			var mouseX:int = evt.currentTarget.mouseX;
			if (mouseX < 0) {
				mouseX = 0;
			} else if (mouseX > _slideWidth) {
				mouseX = _slideWidth;
			}
			_refer.seek(NumberUtilities.getCurrent(checkLimitSlide(mouseX), 0, _slideWidth, 0, _refer.duration));
		}
		private function onSlideUp(evt:MouseEvent):void
		{
			_be_seek = false;
			if (_refer.curmode == VideoPlayer.MODE_PLAY) _refer.stream.resume();		
			_slideBt.removeEventListener(Event.ENTER_FRAME, onSlideMove);
			_stage.removeEventListener(MouseEvent.MOUSE_UP, onSlideUp);
		}
		/**
		 * slidebar not over bufferbar when slide control
		 */
		private function checkLimitSlide($mx:Number):Number
		{
			var limit:Number = $mx;
			if ($mx > _buffer.width - _bufferLimit) limit = _buffer.width - _bufferLimit;
			return limit;
		}
	}
}